home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #1 / Amiga Plus 1999 #1.iso / System-Boost / Graphics / FlashMandel / Sources / Modules / ColorWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-28  |  34.9 KB  |  875 lines

  1. /*******************************************************************************
  2. **
  3. **  Coded by Dino Papararo                 15-May-1998
  4. **
  5. *******************************************************************************/
  6.  
  7. #define __USE_SYSBASE
  8.  
  9. #include <math.h>
  10. #include <string.h>
  11. #include <exec/types.h>
  12. #include <exec/execbase.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/gadgetclass.h>
  15. #include <proto/exec.h>
  16. #include <proto/intuition.h>
  17. #include <proto/graphics.h>
  18. #include <proto/gadtools.h>
  19. #include <proto/utility.h>
  20.  
  21. #define PALETTE      (0L)
  22. #define ACCEPT       (1L)
  23. #define RESET        (2L)
  24. #define CANCEL       (3L)
  25. #define RED          (4L)
  26. #define GREEN        (5L)
  27. #define BLUE         (6L)
  28. #define COPY         (7L)
  29. #define SWAP         (8L)
  30. #define SPREAD       (9L)
  31. #define UNDO         (10L)
  32. #define INVERT       (11L)
  33.  
  34. #define STARTPEN     (4L)
  35.  
  36. #define MINVALUE     (0L)
  37. #define MAXVALUE     (255L)
  38. #define DELTA        (10L)
  39.  
  40. BOOL ModifyPalette  (struct Window *,WORD,WORD,WORD,WORD,ULONG *);
  41. VOID KeepPalette    (struct Window *);
  42. VOID Copy           (struct Window *,ULONG);
  43. VOID Paste          (struct Window *,const ULONG);
  44. VOID Swap           (struct Window *,const ULONG,const ULONG);
  45. BOOL Spread         (struct Window *,const ULONG,const ULONG);
  46. VOID InvertPalette  (struct Window *,ULONG,ULONG);
  47.  
  48. IMPORT struct ExecBase *SysBase;
  49. IMPORT BOOL CheckEHB (ULONG);
  50.  
  51. ULONG COLOR_RGB [3L * 256L + 2L],UNDO_RGB [3L * 256L + 2L],COPY_RGB [3L];
  52.  
  53. struct NewGadget BUTTON_GAD,SLIDER_GAD,PALETTE_GAD;
  54.  
  55. BOOL ModifyPalette (struct Window *Win,WORD LeftEdge,WORD TopEdge,WORD Width,WORD Height,ULONG *Palette32)
  56. {
  57. struct Window *ColorWin;
  58.  
  59. struct IntuiMessage *Message;
  60.  
  61. struct Gadget *GadList = NULL,*MyButtonGad,*MyPaletteGad;
  62.  
  63. struct Gadget *RedSliderGad,*GreenSliderGad,*BlueSliderGad,*MyGad;
  64.  
  65. BOOL Copy_Msg = FALSE,Swap_Msg = FALSE,Spread_Msg = FALSE,Exit = FALSE;
  66.  
  67. ULONG SelectedPen = STARTPEN,OldPen = NULL,NewPen;
  68.  
  69. UWORD MyCode;
  70.  
  71. ULONG MyClass,Colors,ColorBase = 3L * STARTPEN + 1L;
  72.  
  73. ULONG RedLevel,GreenLevel,BlueLevel;
  74.  
  75. CPTR *VInfo;
  76.  
  77.   Colors = 1L << Win->RPort->BitMap->Depth;
  78.  
  79.   if (Colors < 2L) return FALSE;
  80.  
  81.   VInfo = GetVisualInfo (Win->WScreen,TAG_END);
  82.  
  83.   if (! VInfo) return FALSE;
  84.  
  85.   if (CheckEHB (GetVPModeID (ViewPortAddress (Win)))) Colors = 32L;
  86.  
  87.   COLOR_RGB [0L] = UNDO_RGB [0L] = Colors << 16L;
  88.  
  89.   COLOR_RGB [3L * Colors + 1L] = UNDO_RGB [3L * Colors + 1L] = NULL;
  90.  
  91.   COPY_RGB [0L] = COPY_RGB [1L] = COPY_RGB [2L] = NULL;
  92.  
  93.   GetRGB32 (ViewPortAddress (Win)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  94.  
  95.   RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  96.  
  97.   GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  98.  
  99.   BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  100.  
  101.   MyButtonGad = CreateContext (&GadList);
  102.  
  103.   BUTTON_GAD.ng_VisualInfo = PALETTE_GAD.ng_VisualInfo = SLIDER_GAD.ng_VisualInfo = VInfo;
  104.  
  105.   BUTTON_GAD.ng_LeftEdge   = 10;
  106.  
  107.   BUTTON_GAD.ng_TopEdge    = 180;
  108.  
  109.   BUTTON_GAD.ng_Width      = 80;
  110.  
  111.   BUTTON_GAD.ng_Height     = 20;
  112.  
  113.   BUTTON_GAD.ng_GadgetText = "_Accept";
  114.  
  115.   BUTTON_GAD.ng_GadgetID   = ACCEPT;
  116.  
  117.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  118.  
  119.   BUTTON_GAD.ng_LeftEdge  += (15 + BUTTON_GAD.ng_Width);
  120.  
  121.   BUTTON_GAD.ng_GadgetText = "Reset";
  122.  
  123.   BUTTON_GAD.ng_GadgetID   = RESET;
  124.  
  125.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,TAG_END);
  126.  
  127.   BUTTON_GAD.ng_LeftEdge  += (15 + BUTTON_GAD.ng_Width);
  128.  
  129.   BUTTON_GAD.ng_GadgetText = "_Cancel";
  130.  
  131.   BUTTON_GAD.ng_GadgetID   = CANCEL;
  132.  
  133.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  134.  
  135.   BUTTON_GAD.ng_TopEdge    = 8;
  136.  
  137.   BUTTON_GAD.ng_GadgetText = "C_opy";
  138.  
  139.   BUTTON_GAD.ng_GadgetID   = COPY;
  140.  
  141.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  142.  
  143.   BUTTON_GAD.ng_TopEdge   += (6 + BUTTON_GAD.ng_Height);
  144.  
  145.   BUTTON_GAD.ng_GadgetText = "S_wap";
  146.  
  147.   BUTTON_GAD.ng_GadgetID   = SWAP;
  148.  
  149.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  150.  
  151.   BUTTON_GAD.ng_TopEdge   += (6 + BUTTON_GAD.ng_Height);
  152.  
  153.   BUTTON_GAD.ng_GadgetText = "_Spread";
  154.  
  155.   BUTTON_GAD.ng_GadgetID   = SPREAD;
  156.  
  157.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  158.  
  159.   BUTTON_GAD.ng_TopEdge   += (6 + BUTTON_GAD.ng_Height);
  160.  
  161.   BUTTON_GAD.ng_GadgetText = "_Invert";
  162.  
  163.   BUTTON_GAD.ng_GadgetID   = INVERT;
  164.  
  165.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  166.  
  167.   BUTTON_GAD.ng_TopEdge   += (6 + BUTTON_GAD.ng_Height);
  168.  
  169.   BUTTON_GAD.ng_GadgetText = "_Undo";
  170.  
  171.   BUTTON_GAD.ng_GadgetID   = UNDO;
  172.  
  173.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  174.  
  175.   SLIDER_GAD.ng_LeftEdge    = 25;
  176.  
  177.   SLIDER_GAD.ng_TopEdge     = 142;
  178.  
  179.   SLIDER_GAD.ng_Width       = 225;
  180.  
  181.   SLIDER_GAD.ng_Height      = 8;
  182.  
  183.   SLIDER_GAD.ng_GadgetText  = "R";
  184.  
  185.   SLIDER_GAD.ng_GadgetID    = RED;
  186.  
  187.   RedSliderGad = CreateGadget (SLIDER_KIND,MyButtonGad,&SLIDER_GAD,GA_RelVerify,TRUE,GTSL_Max,MAXVALUE,GTSL_Level,(WORD) RedLevel,GTSL_LevelFormat,"%03ld",GTSL_MaxLevelLen,3,GTSL_LevelPlace,PLACETEXT_RIGHT,TAG_END);
  188.  
  189.   SLIDER_GAD.ng_TopEdge    += (4 + SLIDER_GAD.ng_Height);
  190.  
  191.   SLIDER_GAD.ng_GadgetText  = "G";
  192.  
  193.   SLIDER_GAD.ng_GadgetID    = GREEN;
  194.  
  195.   GreenSliderGad = CreateGadget (SLIDER_KIND,RedSliderGad,&SLIDER_GAD,GA_RelVerify,TRUE,GTSL_Max,MAXVALUE,GTSL_Level,(WORD) GreenLevel,GTSL_LevelFormat,"%03ld",GTSL_MaxLevelLen,3,GTSL_LevelPlace,PLACETEXT_RIGHT,TAG_END);
  196.  
  197.   SLIDER_GAD.ng_TopEdge    += (4 + SLIDER_GAD.ng_Height);
  198.  
  199.   SLIDER_GAD.ng_GadgetText  = "B";
  200.  
  201.   SLIDER_GAD.ng_GadgetID    = BLUE;
  202.  
  203.   BlueSliderGad = CreateGadget (SLIDER_KIND,GreenSliderGad,&SLIDER_GAD,GA_RelVerify,TRUE,GTSL_Max,MAXVALUE,GTSL_Level,(WORD) BlueLevel,GTSL_LevelFormat,"%03ld",GTSL_MaxLevelLen,3,GTSL_LevelPlace,PLACETEXT_RIGHT,TAG_END);
  204.  
  205.   PALETTE_GAD.ng_LeftEdge   = 10;
  206.  
  207.   PALETTE_GAD.ng_TopEdge    = 5;
  208.  
  209.   PALETTE_GAD.ng_Width      = 185;
  210.  
  211.   PALETTE_GAD.ng_Height     = 132;
  212.  
  213.   PALETTE_GAD.ng_GadgetID   = PALETTE;
  214.  
  215.   MyPaletteGad = CreateGadget (PALETTE_KIND,BlueSliderGad,&PALETTE_GAD,GTPA_Color,4,GTPA_IndicatorWidth,10,GTPA_NumColors,(UWORD) Colors,TAG_END);
  216.  
  217.   if (! MyPaletteGad)
  218.   {
  219.      FreeGadgets (GadList);
  220.  
  221.      FreeVisualInfo (VInfo);
  222.  
  223.      return FALSE;
  224.   }
  225.  
  226.   ColorWin = OpenWindowTags (0,WA_Left,LeftEdge,
  227.                                WA_Top,TopEdge,
  228.                                WA_Width,Width,
  229.                                WA_Height,Height,
  230.                                WA_Title,"Palette requester",
  231.                                WA_ScreenTitle,"Modify palette...",
  232.                                WA_CustomScreen,Win->WScreen,
  233.                                WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW|IDCMP_VANILLAKEY|IDCMP_GADGETDOWN|BUTTONIDCMP|SLIDERIDCMP|PALETTEIDCMP,
  234.                                WA_Flags,WFLG_ACTIVATE|WFLG_DRAGBAR|WFLG_SIMPLE_REFRESH|WFLG_RMBTRAP|WFLG_GIMMEZEROZERO,
  235.                                WA_Gadgets,GadList,
  236.                                TAG_END);
  237.  
  238.   if (! ColorWin)
  239.   {
  240.      FreeGadgets (GadList);
  241.  
  242.      FreeVisualInfo (VInfo);
  243.  
  244.      return FALSE;
  245.   }
  246.  
  247.   GT_RefreshWindow (ColorWin,NULL);
  248.  
  249.   do { WaitPort (ColorWin->UserPort);
  250.  
  251.        Message = (struct IntuiMessage *) GT_GetIMsg (ColorWin->UserPort);
  252.  
  253.        MyGad   = (struct Gadget *) Message->IAddress;
  254.  
  255.        MyClass = Message->Class;
  256.  
  257.        MyCode  = Message->Code;
  258.  
  259.        GT_ReplyIMsg ((struct IntuiMessage *) Message);
  260.  
  261.        switch (MyClass)
  262.        {
  263.               case IDCMP_REFRESHWINDOW : GT_BeginRefresh (ColorWin);
  264.  
  265.                                          GT_EndRefresh   (ColorWin,TRUE);
  266.  
  267.                                          break;
  268.  
  269.               case IDCMP_VANILLAKEY    : switch (MyCode)
  270.                                          {
  271.                                                  case 'a' :
  272.                                                  case 'A' : GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&Palette32 [1L]);
  273.  
  274.                                                             Exit = TRUE;
  275.  
  276.                                                             break;
  277.  
  278.                                                  case 'c' :
  279.                                                  case 'C' : LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  280.  
  281.                                                             Exit = TRUE;
  282.  
  283.                                                             break;
  284.  
  285.                                                  case 'o' :
  286.                                                  case 'O' : Copy (ColorWin,SelectedPen);
  287.  
  288.                                                             Copy_Msg = TRUE;
  289.  
  290.                                                             Swap_Msg = FALSE;
  291.  
  292.                                                             Spread_Msg = FALSE;
  293.  
  294.                                                             break;
  295.  
  296.                                                  case 'w' :
  297.                                                  case 'W' : OldPen = SelectedPen;
  298.  
  299.                                                             Copy_Msg = FALSE;
  300.  
  301.                                                             Swap_Msg = TRUE;
  302.  
  303.                                                             Spread_Msg = FALSE;
  304.  
  305.                                                             break;
  306.  
  307.                                                  case 's' :
  308.                                                  case 'S' : OldPen = SelectedPen;
  309.  
  310.                                                             Copy_Msg = FALSE;
  311.  
  312.                                                             Swap_Msg = FALSE;
  313.  
  314.                                                             Spread_Msg = TRUE;
  315.  
  316.                                                             break;
  317.  
  318.                                                  case 'u' :
  319.                                                  case 'U' : LoadRGB32 (ViewPortAddress (ColorWin),UNDO_RGB);
  320.  
  321.                                                             GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  322.  
  323.                                                             RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  324.  
  325.                                                             GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  326.  
  327.                                                             BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  328.  
  329.                                                             GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  330.  
  331.                                                             GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  332.  
  333.                                                             GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  334.  
  335.                                                             break;
  336.  
  337.                                                  case 'E' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  338.  
  339.                                                             if (RedLevel > MINVALUE)
  340.                                                             {
  341.                                                                KeepPalette (Win);
  342.  
  343.                                                                RedLevel = (RedLevel < (MINVALUE + DELTA)) ? MINVALUE : RedLevel - DELTA;
  344.  
  345.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  346.  
  347.                                                                COLOR_RGB [ColorBase] = RedLevel << 24L;
  348.  
  349.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  350.                                                             }
  351.  
  352.                                                             break;
  353.  
  354.                                                  case 'e' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  355.  
  356.                                                             if (RedLevel > MINVALUE)
  357.                                                             {
  358.                                                                KeepPalette (Win);
  359.  
  360.                                                                RedLevel--;
  361.  
  362.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  363.  
  364.                                                                COLOR_RGB [ColorBase] = RedLevel << 24L;
  365.  
  366.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  367.                                                             }
  368.  
  369.                                                             break;
  370.  
  371.                                                  case 'T' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  372.  
  373.                                                             if (RedLevel < MAXVALUE)
  374.                                                             {
  375.                                                                KeepPalette (Win);
  376.  
  377.                                                                RedLevel += DELTA;
  378.  
  379.                                                                if (RedLevel > MAXVALUE) RedLevel = MAXVALUE;
  380.  
  381.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  382.  
  383.                                                                COLOR_RGB [ColorBase] = RedLevel << 24L;
  384.  
  385.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  386.                                                             }
  387.  
  388.                                                             break;
  389.  
  390.                                                  case 't' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  391.  
  392.                                                             if (RedLevel < MAXVALUE)
  393.                                                             {
  394.                                                                KeepPalette (Win);
  395.  
  396.                                                                RedLevel++;
  397.  
  398.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  399.  
  400.                                                                COLOR_RGB [ColorBase] = RedLevel << 24L;
  401.  
  402.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  403.                                                             }
  404.  
  405.                                                             break;
  406.  
  407.                                                  case 'F' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  408.  
  409.                                                             if (GreenLevel > MINVALUE)
  410.                                                             {
  411.                                                                KeepPalette (Win);
  412.  
  413.                                                                GreenLevel = (GreenLevel < (MINVALUE + DELTA)) ? MINVALUE : GreenLevel - DELTA;
  414.  
  415.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  416.  
  417.                                                                COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  418.  
  419.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  420.                                                             }
  421.  
  422.                                                             break;
  423.  
  424.                                                  case 'f' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  425.  
  426.                                                             if (GreenLevel > MINVALUE)
  427.                                                             {
  428.                                                                KeepPalette (Win);
  429.  
  430.                                                                GreenLevel--;
  431.  
  432.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  433.  
  434.                                                                COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  435.  
  436.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  437.                                                             }
  438.  
  439.                                                             break;
  440.  
  441.                                                  case 'H' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  442.  
  443.                                                             if (GreenLevel < MAXVALUE)
  444.                                                             {
  445.                                                                KeepPalette (Win);
  446.  
  447.                                                                GreenLevel += DELTA;
  448.  
  449.                                                                if (GreenLevel > MAXVALUE) GreenLevel = MAXVALUE;
  450.  
  451.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  452.  
  453.                                                                COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  454.  
  455.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  456.                                                             }
  457.  
  458.                                                             break;
  459.  
  460.                                                  case 'h' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  461.  
  462.                                                             if (GreenLevel < MAXVALUE)
  463.                                                             {
  464.                                                                KeepPalette (Win);
  465.  
  466.                                                                GreenLevel++;
  467.  
  468.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  469.  
  470.                                                                COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  471.  
  472.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  473.                                                             }
  474.  
  475.                                                             break;
  476.  
  477.                                                  case 'V' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  478.  
  479.                                                             if (BlueLevel > MINVALUE)
  480.                                                             {
  481.                                                                KeepPalette (Win);
  482.  
  483.                                                                BlueLevel = (BlueLevel < (MINVALUE + DELTA)) ? MINVALUE : BlueLevel - DELTA;
  484.  
  485.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  486.  
  487.                                                                COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  488.  
  489.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  490.                                                             }
  491.  
  492.                                                             break;
  493.  
  494.                                                  case 'v' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  495.  
  496.                                                             if (BlueLevel > MINVALUE)
  497.                                                             {
  498.                                                                KeepPalette (Win);
  499.  
  500.                                                                BlueLevel--;
  501.  
  502.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  503.  
  504.                                                                COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  505.  
  506.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  507.                                                             }
  508.  
  509.                                                             break;
  510.  
  511.                                                  case 'N' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  512.  
  513.                                                             if (BlueLevel < MAXVALUE)
  514.                                                             {
  515.                                                                KeepPalette (Win);
  516.  
  517.                                                                BlueLevel += DELTA;
  518.  
  519.                                                                if (BlueLevel > MAXVALUE) BlueLevel = MAXVALUE;
  520.  
  521.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  522.  
  523.                                                                COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  524.  
  525.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  526.                                                             }
  527.  
  528.                                                             break;
  529.  
  530.                                                  case 'n' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  531.  
  532.                                                             if (BlueLevel < MAXVALUE)
  533.                                                             {
  534.                                                                KeepPalette (Win);
  535.  
  536.                                                                BlueLevel++;
  537.  
  538.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  539.  
  540.                                                                COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  541.  
  542.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  543.                                                             }
  544.  
  545.                                                             break;
  546.  
  547.                                                  case 'I' :
  548.                                                  case 'i' : KeepPalette (Win);
  549.  
  550.                                                             InvertPalette (Win,STARTPEN,Colors - 1L);
  551.  
  552.                                                             RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  553.  
  554.                                                             GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  555.  
  556.                                                             BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  557.  
  558.                                                             GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  559.  
  560.                                                             GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  561.  
  562.                                                             GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  563.  
  564.                                                             break;
  565.                                          }
  566.  
  567.                                          break;
  568.  
  569.               case IDCMP_GADGETUP      : switch (MyGad->GadgetID)
  570.                                          {
  571.                                                  case ACCEPT  : GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&Palette32 [1L]);
  572.  
  573.                                                                 Exit = TRUE;
  574.  
  575.                                                                 break;
  576.  
  577.                                                  case RESET   : KeepPalette (Win);
  578.  
  579.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  580.  
  581.                                                                 GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  582.  
  583.                                                                 RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  584.  
  585.                                                                 GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  586.  
  587.                                                                 BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  588.  
  589.                                                                 GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  590.  
  591.                                                                 GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  592.  
  593.                                                                 GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  594.  
  595.                                                                 break;
  596.  
  597.                                                  case CANCEL  : LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  598.  
  599.                                                                 Exit = TRUE;
  600.  
  601.                                                                 break;
  602.  
  603.                                                  case COPY    : Copy (ColorWin,SelectedPen);
  604.  
  605.                                                                 Copy_Msg = TRUE;
  606.  
  607.                                                                 Swap_Msg = FALSE;
  608.  
  609.                                                                 Spread_Msg = FALSE;
  610.  
  611.                                                                 break;
  612.  
  613.                                                  case SWAP    : OldPen = SelectedPen;
  614.  
  615.                                                                 Copy_Msg = FALSE;
  616.  
  617.                                                                 Swap_Msg = TRUE;
  618.  
  619.                                                                 Spread_Msg = FALSE;
  620.  
  621.                                                                 break;
  622.  
  623.                                                  case SPREAD  : OldPen = SelectedPen;
  624.  
  625.                                                                 Copy_Msg = FALSE;
  626.  
  627.                                                                 Swap_Msg = FALSE;
  628.  
  629.                                                                 Spread_Msg = TRUE;
  630.         
  631.                                                                 break;
  632.  
  633.                                                  case INVERT  : KeepPalette (Win);
  634.  
  635.                                                                 InvertPalette (Win,STARTPEN,Colors - 1L);
  636.  
  637.                                                                 RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  638.  
  639.                                                                 GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  640.  
  641.                                                                 BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  642.  
  643.                                                                 GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  644.  
  645.                                                                 GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  646.  
  647.                                                                 GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  648.  
  649.                                                                 break;
  650.  
  651.                                                  case UNDO    : LoadRGB32 (ViewPortAddress (ColorWin),UNDO_RGB);
  652.  
  653.                                                                 GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  654.  
  655.                                                                 RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  656.  
  657.                                                                 GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  658.  
  659.                                                                 BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  660.  
  661.                                                                 GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  662.  
  663.                                                                 GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  664.  
  665.                                                                 GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  666.  
  667.                                                                 break;
  668.  
  669.                                                  case RED     : KeepPalette (Win);
  670.  
  671.                                                                 GT_GetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,&RedLevel);
  672.  
  673.                                                                 COLOR_RGB [ColorBase]      = RedLevel << 24L;
  674.  
  675.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  676.  
  677.                                                                 break;
  678.  
  679.                                                  case GREEN   : KeepPalette (Win);
  680.  
  681.                                                                 GT_GetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,&GreenLevel);
  682.  
  683.                                                                 COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  684.  
  685.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  686.  
  687.                                                                 break;
  688.  
  689.                                                  case BLUE    : KeepPalette (Win);
  690.  
  691.                                                                 GT_GetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,&BlueLevel);
  692.  
  693.                                                                 COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  694.  
  695.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  696.  
  697.                                                                 break;
  698.  
  699.                                                  case PALETTE : GT_GetGadgetAttrs (MyPaletteGad,ColorWin,NULL,GTPA_Color,&SelectedPen);
  700.  
  701.                                                                 if (Copy_Msg)
  702.                                                                 {
  703.                                                                    KeepPalette (Win);
  704.  
  705.                                                                    Paste (ColorWin,SelectedPen);
  706.  
  707.                                                                    Copy_Msg = FALSE;
  708.                                                                 }
  709.  
  710.                                                                 if (Swap_Msg)
  711.                                                                 {
  712.                                                                    KeepPalette (Win);
  713.  
  714.                                                                    NewPen = SelectedPen;
  715.  
  716.                                                                    Swap (ColorWin,OldPen,NewPen);
  717.  
  718.                                                                    Swap_Msg = FALSE;
  719.                                                                 }
  720.  
  721.                                                                 if (Spread_Msg)
  722.                                                                 {
  723.                                                                    KeepPalette (Win);
  724.  
  725.                                                                    NewPen = SelectedPen;
  726.  
  727.                                                                    Spread (ColorWin,OldPen,NewPen);
  728.  
  729.                                                                    Spread_Msg = FALSE;
  730.                                                                 }
  731.  
  732.                                                                 ColorBase = 3L * SelectedPen + 1L;
  733.  
  734.                                                                 RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  735.  
  736.                                                                 GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  737.  
  738.                                                                 BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  739.  
  740.                                                                 GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  741.  
  742.                                                                 GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  743.  
  744.                                                                 GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  745.  
  746.                                                                 break;
  747.                                          }
  748.  
  749.                                          break;
  750.  
  751.               case IDCMP_CLOSEWINDOW   : Exit = TRUE;
  752.  
  753.                                          break;
  754.        }
  755.  
  756.      } while (Exit == FALSE);
  757.  
  758.   CloseWindow (ColorWin);
  759.  
  760.   FreeGadgets (GadList);
  761.  
  762.   FreeVisualInfo (VInfo);
  763.  
  764.   return TRUE;
  765. }
  766.  
  767. VOID Copy (struct Window *Win,ULONG PenNumber)
  768. {
  769.   GetRGB32 (ViewPortAddress (Win)->ColorMap,PenNumber,1L,COPY_RGB);
  770. }
  771.  
  772. VOID Paste (struct Window *Win,const ULONG PenNumber)
  773. {
  774. const ULONG ColorBase = 3L * PenNumber + 1L;
  775.  
  776. ULONG Index;
  777.  
  778.   for (Index = 0L; Index < 3L; Index++) COLOR_RGB [ColorBase + Index] = COPY_RGB [Index];
  779.  
  780.   LoadRGB32 (ViewPortAddress (Win),COLOR_RGB);
  781. }
  782.  
  783. VOID Swap (struct Window *Win,const ULONG OldPen,const ULONG NewPen)
  784. {
  785. ULONG Tmp_RGB [3L];
  786.  
  787. ULONG Index;
  788.  
  789.    GetRGB32 (ViewPortAddress (Win)->ColorMap,OldPen,1L,Tmp_RGB);
  790.  
  791.    Copy (Win,NewPen);
  792.  
  793.    Paste (Win,OldPen);
  794.  
  795.    for (Index = 0L; Index < 3L; Index++) COPY_RGB [Index] = Tmp_RGB [Index];
  796.  
  797.    Paste (Win,NewPen);
  798. }
  799.  
  800. BOOL Spread (struct Window *Win,const ULONG OldPen,const ULONG NewPen)
  801. {
  802. const ULONG StartPen = min (OldPen,NewPen) , EndPen = max (OldPen,NewPen);
  803.  
  804. const ULONG  Range   = EndPen - StartPen;
  805.  
  806. LONG RedStep,GreenStep,BlueStep;
  807.  
  808. ULONG Index,RedLevel,GreenLevel,BlueLevel,ColorBase;
  809.  
  810.   if (Range < 2L) return TRUE;
  811.  
  812.   ColorBase   = 3L * StartPen + 1L;
  813.  
  814.   RedLevel    = COLOR_RGB [ColorBase]      >> 8L;
  815.  
  816.   GreenLevel  = COLOR_RGB [ColorBase + 1L] >> 8L;
  817.  
  818.   BlueLevel   = COLOR_RGB [ColorBase + 2L] >> 8L;
  819.  
  820.   ColorBase   = 3L * EndPen + 1L;
  821.  
  822.   RedStep     = (LONG) (COLOR_RGB [ColorBase]      >> 8L);
  823.  
  824.   GreenStep   = (LONG) (COLOR_RGB [ColorBase + 1L] >> 8L);
  825.  
  826.   BlueStep    = (LONG) (COLOR_RGB [ColorBase + 2L] >> 8L);
  827.  
  828.   RedStep    -= (LONG) RedLevel;
  829.  
  830.   GreenStep  -= (LONG) GreenLevel;
  831.  
  832.   BlueStep   -= (LONG) BlueLevel;
  833.  
  834.   RedStep    /= (LONG) Range;
  835.  
  836.   GreenStep  /= (LONG) Range;
  837.  
  838.   BlueStep   /= (LONG) Range;
  839.  
  840.   for (Index = (StartPen + 1L); Index < EndPen; Index++)
  841.   {
  842.       RedLevel   += RedStep;
  843.  
  844.       GreenLevel += GreenStep;
  845.  
  846.       BlueLevel  += BlueStep;
  847.  
  848.       ColorBase   = 3L * Index + 1L;
  849.  
  850.       COLOR_RGB [ColorBase]      = RedLevel   << 8L;
  851.  
  852.       COLOR_RGB [ColorBase + 1L] = GreenLevel << 8L;
  853.  
  854.       COLOR_RGB [ColorBase + 2L] = BlueLevel  << 8L;
  855.   }
  856.  
  857.   LoadRGB32 (ViewPortAddress (Win),COLOR_RGB);
  858.  
  859.   return FALSE;
  860. }
  861.  
  862. VOID KeepPalette (struct Window *Win)
  863. {
  864. ULONG Colors;
  865.  
  866.   Colors = (CheckEHB (GetVPModeID (ViewPortAddress (Win))) ? 32L : 1L << Win->RPort->BitMap->Depth);
  867.  
  868.   GetRGB32 (ViewPortAddress (Win)->ColorMap,0L,Colors,&UNDO_RGB [1L]);
  869. }
  870.  
  871. VOID InvertPalette (struct Window *Win,ULONG StartPen,ULONG EndPen)
  872. {
  873.   while (StartPen < EndPen) Swap (Win,StartPen++,EndPen--);
  874. }
  875.